package ypmits.components.buttons {
	import flash.events.MouseEvent;
	import ypmits.components.Component;
	import ypmits.components.ComponentSkin;
	import ypmits.components.util.TextFieldUtility;
	import ypmits.font.Ronda;

	import flash.filters.BevelFilter;
	import flash.text.TextFormatAlign;

	/**
	 * @author Dave Lenz
	 */
	public class BasicButton extends Component {

		private var label : TextFieldUtility;

		
		
		public function BasicButton() 
		{
			var s : ComponentSkin = new ComponentSkin();
			s.width = 120;
			s.height = 22;
			s.backgroundcolor = 0xFFFFFF;
			
			super( s );
			
			draw();
			
			addEventListener( MouseEvent.MOUSE_OVER, mouseHandler );
			addEventListener( MouseEvent.MOUSE_OUT, mouseHandler );
			addEventListener( MouseEvent.MOUSE_DOWN, mouseHandler );
			addEventListener( MouseEvent.MOUSE_UP, mouseHandler );
		}

		
		
		private function mouseHandler( e : MouseEvent ) : void 
		{
			if( e.type == MouseEvent.MOUSE_OVER ) {
				skin.backgroundcolor = 0xA1A1A1;
			} else if( e.type == MouseEvent.MOUSE_OUT ) {
				skin.backgroundcolor = 0xFFFFFF;
				filters = [ new BevelFilter( 1, 45, 0xFFFFFF, 1, 0, .3, 2, 2, 1, 3 ) ];
			} else if( e.type == MouseEvent.MOUSE_DOWN ) {
				filters = [ new BevelFilter( 1, 45, 0, 1, 0xFFFFFF, .3, 2, 2, 1, 3 ) ];
			} else if( e.type == MouseEvent.MOUSE_UP ) {
				filters = [ new BevelFilter( 1, 45, 0xFFFFFF, 1, 0, .3, 2, 2, 1, 3 ) ];
			}
			
			redraw();
		}

		
		
		private function draw() : void 
		{
			label = new TextFieldUtility( "", new Ronda().fontName, 8, 0, false, TextFormatAlign.CENTER, true );
			addChild( label );

			buttonMode = true;
			
			filters = [ new BevelFilter( 1, 45, 0xFFFFFF, 1, 0, .3, 2, 2, 1, 3 ) ];
			
			redraw( );
		}

		
		
		private function redraw() : void 
		{
			graphics.clear( );
			graphics.lineStyle( 1, 0, 1, true );
			graphics.beginFill( skin.backgroundcolor, 1 );
			graphics.drawRect( 0, 0, skin.width, skin.height );
			graphics.endFill();
			
			label.width = skin.width;
			label.x = Math.ceil( label.x );
			label.y = (skin.height * .5) - (label.height * .5) - 2;
		}

		
		
		override public function enable() : void {
			super.enable();
			alpha = 1;
		}
		
		
		override public function disable() : void {
			super.disable();
			alpha = .5;
		}

		
		
		override public function set width( value : Number ) : void {
			skin.width = value;

			redraw();
		}

		
		
		override public function set height( value : Number ) : void {
			skin.height = value;
			
			redraw();
		}

		
		
		override public function get text() : String 
		{
			return _text;
		}


		override public function set text( value : String ) : void {
			if(label) label.text = value;
			_text = value;
		}
	}
}
